
 var commonPasswords = new Array('password','asdfgf','qwerty', 'pass', '1234', '1246');
 var numbers = "0123456789";
 var lowercase = "abcdefghijklmnopqrstuvwxyz";
 var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 var punctuation = "!.@$£#*()%~<>{}[]";

function validate_form ( )
{
	valid = true;

         if ( document.myform.uname.value == "" )
        {
                alert ( "Empty Name" );
                valid = false;
        }else if ( document.myform.regno.value == "" )
        {
                alert ( "Enter Regno" );
                valid = false;
        }else if ( document.myform.pass.value == "" )
        {
                alert ( "Empty Password" );
                valid = false;
        }else if( document.myform.mac.value=="")
        {
                 alert("Enter MAC address");
                 valid = false;
        }else if(maccheck(document.myform.mac.value)==false){
		         document.myform.mac.value=""
		         document.myform.mac.focus()
                 valid= false;
    	}else if( ( document.myform.gender[0].checked == false ) && ( document.myform.gender[1].checked == false ) )
        {
                alert ( "Choose your Gender " );
                valid = false;
        }else   if(( document.myform.email.value==null)||(document.myform.email.value=="")){
                alert("Enter Email ID ")
	            document.myform.email.focus()
                valid= false;
     	} else  if (document.myform.res.selectedIndex == 0) {
                  alert("Select your Residence.");
                  document.myform.res.focus()
                  valid= false;
         }else if( document.myform.room_no.value=="")
        {
                 alert("Enter Room no:");
                 valid = false;
        }else if(roomcheck(document.myform.room_no.value)==false){
		         document.myform.room_no.value=""
		         document.myform.room_no.focus()
                 valid= false;
    	}

        return valid;
}
function isAlphabet(elem){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert("Enter Name");
		elem.value="";
		elem.focus();
		return false;
	}
}

function emailValidator(elem){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert("Enter vaild email id");
		elem.focus();
		return false;
	}
}
function roomcheck(rooNo)
      {
       var RegExPattern = /^[0-9]+$/;
       if (!(rooNo.match(RegExPattern)) || rooNo.length >3)
         {
          alert("Invalid  Room No");
          return false;
         }
      }
function maccheck(macAdd)
      {
       var RegExPattern = /^[0-9a-fA-F:/-]+$/;
       if (!(macAdd.match(RegExPattern)) || macAdd.length != 17)
         {
          alert("Invalid Media Access Control Address");
          return false;
         }
      }

function checkPassword(password) {

    var combinations = 0;

    if (contains(password, numbers) > 0) {
        combinations += 10;
    }

    if (contains(password, lowercase) > 0) {
        combinations += 26;
    }

    if (contains(password, uppercase) > 0) {
        combinations += 26;
    }

    if (contains(password, punctuation) > 0) {
        combinations += punctuation.length;
    }


    var totalCombinations = Math.pow(combinations, password.length);


    if (isCommonPassword(password)) {
        totalCombinations = 75000 // about the size of the dictionary
    }


    var timeInSeconds = (totalCombinations / 800) / 2;


    var timeInDays = timeInSeconds / 86400


    var lifetime = 365;


    var percentage = timeInDays / lifetime;

    var friendlyPercentage = cap(Math.round(percentage * 100), 100);
    if (totalCombinations != 75000 && friendlyPercentage < (password.length * 5)) {
        friendlyPercentage += password.length * 5;
    }

    var progressBar = document.getElementById("progressBar");
    progressBar.style.width = friendlyPercentage + "%";

    if (percentage > 1) {

        progressBar.style.backgroundColor = "#3bce08";
        return;
    }

    if (percentage > 0.5) {

        progressBar.style.backgroundColor = "#ffd801";
        return;
    }

    if (percentage > 0.10) {

        progressBar.style.backgroundColor = "orange";
        return;
    }


    if (percentage <= 0.10) {

        progressBar.style.backgroundColor = "red";
        return;
    }


}

function cap(number, max) {
    if (number > max) {
        return max;
    } else {
        return number;
    }
}

function isCommonPassword(password) {

    for (i = 0; i < commonPasswords.length; i++) {
        var commonPassword = commonPasswords[i];
        if (password == commonPassword) {
            return true;
        }
    }

    return false;

}

function contains(password, validChars) {

    count = 0;

    for (i = 0; i < password.length; i++) {
        var char = password.charAt(i);
        if (validChars.indexOf(char) > -1) {
            count++;
        }
    }

    return count;
}

function recheckPassword(element) {
         if (document.myform.pass.value != document.myform.rpass.value) {
            alert ("Passwords do not match. Please re-enter.");
            document.myform.pass.value="";
            document.myform.rpass.value="";
            document.myform.pass.focus();
         return false;
                }
}


